import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'FavListTitle.dart';
import 'color/MyColor.dart';
import 'color/MyEncrypt.dart';
import 'model/BaseModel.dart';
import 'dart:convert' as convert;
import 'model/VideoInfo.dart';
import 'color/MyNetTool.dart';
import 'view/AppNavi.dart';
class Video extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
      ),
      home:VideoCtrl(title: "视频",),
    );
  }
}
class VideoCtrl extends StatefulWidget{
  VideoCtrl({Key key, this.title}) : super(key: key);

  final String title;
  @override
  State<StatefulWidget> createState() {
    return _VideoCtrlState();
  }
}

class _VideoCtrlState extends State<VideoCtrl> {
  var listData = <VideoInfo>[];
  var page     = 0;
  void initState(){
    super.initState();
    loadData();
  }
  Future <void> onRefresh() async {
    await Future.delayed(Duration(seconds: 2), (){
      page = 0;
      listData.clear();
      loadData();
    });
  }
  void loadData() async{
    Map map = {"page":1};
    Map params = await MyEncrypt.encryptionParams(map, EncryptionState.encryption);
    print(params);
    Map<String ,dynamic> object = await MyNetTool.method(HttpMethod.Post, AppBaseUrl2, "/index/index", params, MyEncrypt.headers());
    BaseModel model = BaseModel.fromJson(object);
    print(model.code);
    if (model.code == 2){
      String decode = await MyEncrypt.decodeString(model.data);
      Map<String, dynamic> jsonString = convert.jsonDecode(decode);
      VideoHome video = VideoHome.fromJson(jsonString);
      this.listData = video.videoHots;
      setState(() {

      });
    }
  }
  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334);
    return Scaffold(
      appBar: AppNavi(
        title:widget.title,
        backgroundColor: MyColor.AppColor,
        textColor: MyColor.AppFFFFFF,
        showBack: false,
      ),

      body: Center(

        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            new Container(
              height: AppHeigh - AppStatusBar - AppNaviBar - AppToolBar - AppBottom,
              width:  AppWidth,
              color: MyColor.AppFFFFFF,
              child: new RefreshIndicator(child: GridView.builder(

                padding: EdgeInsets.all(10.0),
                itemCount: this.listData.length,
                itemBuilder: (context,index){
                  VideoInfo model = this.listData[index];
                  var count = model.likeCount;
                  var url = model.cover;
                  var subTitle =  "喜欢 : $count";
                  return HomeListTitle(title: model.title,subTitle: model.category,url: url,onTap: (){

                  });

                },
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisSpacing: 10,
                  mainAxisSpacing: 10,
                  crossAxisCount: 3,
                  childAspectRatio: 0.5,

                ),
              ), onRefresh: onRefresh),

            ),
          ],
        ),
      ),
    );
  }
}